package com.cash.channel.svc.service.common;

import com.cash.channel.api.model.cycle.AmountLimit;
import com.cash.channel.svc.logic.OrderLogic;
import com.cash.channel.svc.logic.RepayLogic;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.service.base.ApplyAuditStatus;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.enums.ApplyAuditStatusEnum;
import com.cash.channel.api.model.request.ApplyAuditStatusRequest;
import com.cash.channel.api.model.response.ApplyAuditStatusResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.查询进件审批结果)
public class CommonApplyAuditStatus implements ApplyAuditStatus {

    @Autowired
    private OrderLogic orderLogic;

    @Autowired
    private RepayLogic repayLogic;

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Override
    public ApiResponseModel<ApplyAuditStatusResponse> doGetApplyAuditStatus(ApiRequestModel apiRequestModel) throws CashChannelException {
        try {
            ApplyAuditStatusRequest applyAuditStatusRequest = JSON.parseObject(apiRequestModel.getData(), ApplyAuditStatusRequest.class);
            ThirdIdModel thirdIdModel = thirdCommonLogic.findThirdIdModelByThirdOrderId(applyAuditStatusRequest.getOrderNo(), apiRequestModel.getChannel());
            Map<String,Object> orderMap = orderLogic.secQCOrderBAAPI(thirdIdModel.getCAppId());
            ApplyAuditStatusResponse applyAuditStatusResponse = getApplyAuditStatusByQcApply(orderMap, apiRequestModel.getChannel());
            return new ApiResponseModel<>(true, 0, "", applyAuditStatusResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CashChannelException("获取额度订单状态失败");
        }
    }

    private ApplyAuditStatusResponse getApplyAuditStatusByQcApply(Map<String, Object> orderMap, String channel) throws Exception {
        String nQCAppStatus = MapUtils.getString(orderMap, "nAppStatus");
        ApplyAuditStatusResponse applyAuditStatusResponse = new ApplyAuditStatusResponse();

        switch (nQCAppStatus) {
            case "300":
                AmountLimit limitAmt = repayLogic.getLimitAmt(MapUtils.getString(orderMap, "cAppId"), MapUtils.getString(orderMap, "cUuid"));
                applyAuditStatusResponse.setStatus(ApplyAuditStatusEnum.APPROVED.getStatus());
                applyAuditStatusResponse.setAmtRange(100);
                applyAuditStatusResponse.setMinLoanAmt(1000);
                applyAuditStatusResponse.setApproveTime(new Date(MapUtils.getLongValue(orderMap, "dUpdate")));
                applyAuditStatusResponse.setMaxLoanAmt(limitAmt.getTotalAmount().intValue());
                applyAuditStatusResponse.setTermOptions(thirdCommonLogic.getLoanTermOption(channel));
                applyAuditStatusResponse.setAmountLimit(limitAmt);
                Boolean flag= thirdCommonLogic.checkOrderStatus(MapUtils.getString(orderMap, "cUuid"),null);
                if(!flag){
                    applyAuditStatusResponse.setStatus(ApplyAuditStatusEnum.FROZEN.getStatus());
                }
                break;
            case "400":
                applyAuditStatusResponse.setStatus(ApplyAuditStatusEnum.REJECTED.getStatus());
                break;
            default:
                applyAuditStatusResponse.setStatus(ApplyAuditStatusEnum.WAITING.getStatus());
                break;
        }

        return applyAuditStatusResponse;
    }



}
